package net.tycmc.cty.ctyms.timejob.iems.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.commons.configuration.ConfigurationException;

import com.tycmc.functions.util.MapGetter;
import com.tycmc.functions.util.NumberFormat;

import net.tycmc.bulb.common.util.DateUtil;
import net.tycmc.bulb.common.util.StringUtil;
import net.tycmc.cty.ctyms.timejob.common.DateControl;
import net.tycmc.cty.ctyms.timejob.common.DynamicReadConfig;
import net.tycmc.cty.ctyms.timejob.common.ListUtil;
import net.tycmc.cty.ctyms.timejob.iems.dao.VclRoadInfoDAO;
import net.tycmc.cty.ctyms.timejob.iems.thread.VclRoadRunner;

public class VclRoadInfoService {
	private VclRoadInfoDAO vclRoadInfoDAO;
	
	public VclRoadInfoDAO getVclRoadInfoDAO(){
		return vclRoadInfoDAO;
	}
	
	public void setVclRoadInfoDAO(VclRoadInfoDAO vclRoadInfoDAO){
		this.vclRoadInfoDAO = vclRoadInfoDAO;
	}
	/**
	 * 手动执行统计作业
	 * @author wangkai
	 * @param Map<String,Object> param 
	 *             param支持Key：workDay-需要重新统计的时间
	 *                         dataBaseIp-数据库连接地址192.168.30.101\GNSSv112
	 *                         user-数据库用户名
	 *                         password-数据库密码
	 *                         MsgTDataBase-Msg_Timing_yyyyMMdd所在数据库名
	 *                         MsgPDataBase-Msg_PGN_yyyyMMdd所在数据库名称
	 *                         databaseVclView-设备视图所在地址192.168.88.252
	 * */
	public void runRoadExecutorService(Map<String,Object> param){
		System.out.println("道路信息统计开始--------"+DateControl.getDateTime(0));
		ExecutorService	executorService = Executors.newFixedThreadPool(10);
		int t=0;
		String isbakTable = "";
		String thisWorkDay = "";
		if(null!=param){//如果param不为空则说明为手动执行统计作业
			thisWorkDay = param.get("workDay").toString().substring(0, 10).replaceAll("-", "");//2015-12-01
			//统计前先将要统计的表中数据删除
			vclRoadInfoDAO.deletVclRoad(thisWorkDay);
		}else{
			try {
				t = Integer.parseInt(DynamicReadConfig.getConfigProperty("config.properties", "roadinfo"));
			} catch (ConfigurationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			thisWorkDay = DateUtil.addDay(-t).replace("-", "");
		}
		try {//获取配置文件中是否需要备份数据的条件1为备份，0为不备份
			isbakTable = DynamicReadConfig.getConfigProperty("config.properties", "VclRoadInfo");
		} catch (ConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		boolean isExit = vclRoadInfoDAO.checkCreateRoadInfo(thisWorkDay,isbakTable);
		if(isExit){
			List<Map<String,Object>> vclList = vclRoadInfoDAO.getVclID(param);
			Map<String,Object> vclMap = new HashMap<String, Object>();
			String VehcileIDS = "";
			for(int i=0;i<vclList.size();i++){
				vclMap = vclList.get(i);
				VehcileIDS = vclMap.get("VehicleID").toString();
				executorService.submit(new VclRoadRunner(VehcileIDS,isbakTable,param));
			}
		}else{
			System.out.println("道路信息日表不存在"+thisWorkDay);
		}
		System.out.println("道路信息统计结束--------"+DateControl.getDateTime(0));
}
	public static void main(String[] args) {
			System.out.println(DateUtil.addDay(-60));
	}

	public List<Map<String,Object>> getVclRoadCount() {
		List<String> list = new ArrayList<String>();
		list.add(DateUtil.addDay(-1));
		list.add(DateUtil.addDay(-2));
		list.add(DateUtil.addDay(-3));
		return vclRoadInfoDAO.getVclRoadInfoCount(list);
		
	}
	//获取正在统计的数据量
	public int getExecuteCount(String isExectue) {
		return vclRoadInfoDAO.getExeVclRoadInfoCount(isExectue);
		
	}

}
